package com.example.demo.controller.zabbix;

import com.example.demo.core.domain.AjaxResult;
import com.example.demo.zabbix.Zabbix;
import com.example.demo.zabbix.request.builder.request.AuthRequestBuilder;
import com.example.demo.zabbix.response.BaseResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
@Configuration
public class  LoginController {

    //缓存socket对象
    public static Map<String, String> cacheData = new HashMap<>();

    public static Zabbix zabbix;

    @Bean
    public void startServer(){
        zabbix = new Zabbix();
        zabbix.init("http://172.16.79.141/zabbix");
        System.out.println("初始化zabbix");
        BaseResponse<String> authResponse = zabbix.call(new AuthRequestBuilder(1L).setUserName("admin").setPassword("admin").builder());
        String auth = authResponse.getResult();
        cacheData.put("token",auth);
        System.out.println("获取用户Token==>"+auth);
    }

    /**
     * 查询用户Token并存入缓存
     */
    @GetMapping("/getAuth")
    public AjaxResult list(){
        String auth = cacheData.get("token");
        return AjaxResult.success(auth);
    }
}
